﻿<%@ WebHandler Language="C#" Class="_1006_2" %>

using System;
using System.Web;
using CCH.Entity;
using CCH.DAO;
using System.IO;
using System.Linq;

public class _1006_2 : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        int alb_no = int.Parse(context.Request["alb_no"]);
        int pho_no = 0;
        int.TryParse(context.Request["pho_no"], out pho_no);


        byte[] img_bin = null;

        using (Entities model = new Entities())
        {
            int count = (from d in model.photo where d.alb_no == alb_no select d).Count();

            if (count > 0)
            {
                string filepath = (from d in model.photo where d.alb_no == alb_no && d.pho_no == pho_no select d.pho_file).FirstOrDefault();

                if (!string.IsNullOrEmpty(filepath))
                {
                    img_bin = File.ReadAllBytes(filepath);
                }
            }
            else
            {
                img_bin = File.ReadAllBytes(context.Server.MapPath("~/images/noimage.jpg"));
            }

        }

        //輸出
        context.Response.ContentType = "image/jpeg";
        context.Response.BinaryWrite(img_bin);
        context.Response.Flush();
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}